package com.ca.ridersclubapplication.view.list;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ridersclubapplication.ClubActivity;
import com.ca.ridersclubapplication.view.card.MiniInfoCard;
import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.data.bean.MiniClubInfo;

import java.util.ArrayList;
import java.util.List;

public class FollowMiniClubListView extends ConstraintLayout {

    private Activity activity;
    private final List<MiniInfoCard> cardList = new ArrayList<>();

    public TextView emptyView;
    public Button gotoLoginButton;
    private List<MiniClubInfo> miniClubInfoList = new ArrayList<>();

    public FollowMiniClubListView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public FollowMiniClubListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public FollowMiniClubListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {

        View view = LayoutInflater.from(context).inflate(R.layout.follow_mini_club_list_layout, this);

        emptyView = view.findViewById(R.id.tv_empty);
        gotoLoginButton = view.findViewById(R.id.btn_gotoLogin);
        gotoLoginButton.setBackgroundColor(getResources().getColor(R.color.primary));

        cardList.add(view.findViewById(R.id.card_miniInfo_1));
        cardList.add(view.findViewById(R.id.card_miniInfo_2));
        cardList.add(view.findViewById(R.id.card_miniInfo_3));
        cardList.add(view.findViewById(R.id.card_miniInfo_4));

        setMiniInfoList(miniClubInfoList);
    }

    public List<MiniClubInfo> getMiniInfoList() {
        return miniClubInfoList;
    }

    public void setMiniInfoList(List<MiniClubInfo> miniClubInfoList) {

        this.miniClubInfoList = miniClubInfoList == null ? new ArrayList<>() : miniClubInfoList;

        emptyView.setVisibility(this.miniClubInfoList.size() == 0 ? VISIBLE : GONE);

        for (int i = 0; i < this.cardList.size(); i++) {

            MiniInfoCard card = getMiniInfoCard(i);

            if (i < this.miniClubInfoList.size()) {
                card.setVisibility(VISIBLE);

                MiniClubInfo info = getMiniInfo(i);
                card.setImageUrl(info.getImageUrl());
                card.setName(info.getName());

                if (activity != null) {
                    card.setOnClickListener(v -> {
                        Intent intent = new Intent(activity, ClubActivity.class);
                        intent.putExtra("clubId", info.getId());
                        activity.startActivity(intent);
                    });
                }
            }
            else {
                card.setVisibility(GONE);
                card.setOnClickListener(v -> {});
            }
        }
    }

    public void flush() {
        setMiniInfoList(getMiniInfoList());
    }

    public MiniInfoCard getMiniInfoCard(int index) {
        return this.cardList.get(index);
    }

    public MiniClubInfo getMiniInfo(int index) {
        return this.miniClubInfoList.get(index);
    }

    public List<MiniInfoCard> getCardList() {
        return cardList;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }
}
